package com.smdk.dsminio.controller;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.smdk.dsminio.comment.BaseController;
import com.smdk.dsminio.entity.DsminioBucketInfo;
import com.smdk.dsminio.request.OnLlineUserInfo;
import com.smdk.dsminio.service.DsminioBucketInfoService;
import com.smdk.dsminio.service.DsminioFileInfoService;
import com.smdk.dsminio.service.DsminioFolderInfoService;
import com.smdk.dsminio.utils.AjaxResult;
import com.smdk.dsminio.vo.UserInfoVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;


/**
 * @author 神秘的凯
 * @since 2020-03-19
 */

@Api(tags = {"BucketInfo"})
@RestController
@RequestMapping("/api/")
public class DsminioBucketInfoController extends BaseController {
    @Autowired
    private DsminioBucketInfoService bucketinfoService;
    @Autowired
    private DsminioFolderInfoService dsminioFolderInfoService;

    /**
     * 创建新的bucket
     *
     * @param
     * @return
     */
    @RequestMapping(value = "/createBucket", method = RequestMethod.POST)
    @ApiOperation(value = "创建Bucket")
    public @ResponseBody
    AjaxResult createBucket(@ApiParam(value = "bucketName", required = true)String bucketName,
                            HttpServletRequest request) {
        UserInfoVO userInfo = OnLlineUserInfo.getUserInfo(request);
        //保存进数据库
        DsminioBucketInfo bucketInfo = new DsminioBucketInfo();
        bucketInfo.setUserId(userInfo.getUserId());
        bucketInfo.setAccessRight(0);
        bucketInfo.setBucketObjectCount(0);
        bucketInfo.setBucketOrder(0);
        bucketInfo.setBucketSize(0L);
        bucketInfo.setBucketName(bucketName);
        bucketInfo.setCreateDateTime(new Date());
        return AjaxResult.addResult(bucketinfoService.createBucketInfo(bucketInfo));
    }


    /**
     * 删除bucket
     *
     * @param
     * @return
     */
    @RequestMapping(value = "/deleteBucket", method = RequestMethod.POST)
    @ApiOperation(value = "删除bucket")
    public @ResponseBody
    AjaxResult deleteBucket(@ApiParam(value = "bucketId", required = true) @RequestParam(required = true, value = "bucketId") Long bucketId) {
        boolean searchFolderCount = dsminioFolderInfoService.searchFolderCount(bucketId);
        if (!searchFolderCount) {
            return AjaxResult.fail("当前Bucket内存在对象信息不能删除！");
        }
        DsminioBucketInfo bucketInfo = bucketinfoService.searchBucketInfoById(bucketId);
        if (bucketInfo == null) {
            return AjaxResult.fail("当前文件夹不存在");
        }
        //删除bucket
        return AjaxResult.deleteResult(bucketinfoService.deleteBucketInfo(bucketId));
    }



    /**
     * 修改Bucket名称
     *
     * @param
     * @return
     */
    @RequestMapping(value = "/updateBucket", method = RequestMethod.POST)
    @ApiOperation(value = "修改Bucket信息")
    public @ResponseBody
    AjaxResult updateBucket(@ApiParam(value = "bucketId", required = true) @RequestParam(required = true, value = "bucketId") Long bucketId,
                            @ApiParam(value = "bucketName", required = true) @RequestParam(required = true, value = "bucketName") String bucketName) {
        DsminioBucketInfo bucketInfo = new DsminioBucketInfo();
        bucketInfo.setBucketName(bucketName);
        bucketInfo.setId(bucketId);
        bucketInfo.setCreateDateTime(new Date());
        return AjaxResult.updateResult(bucketinfoService.updateBucketInfo(bucketInfo));
    }




    /**
     * 查询Bucket信息
     *
     * @param
     * @return
     */
    @RequestMapping(value = "/searchBucketList", method = RequestMethod.GET)
    @ApiOperation(value = "查询Bucket信息")
    public @ResponseBody
    AjaxResult searchBucketList(@ApiParam(value = "分页参数", required = true) @RequestParam(required = true, value = "startPage",defaultValue = "0") Integer startPage,HttpServletRequest request){
        UserInfoVO userInfoVO=OnLlineUserInfo.getUserInfo(request);
        PageHelper.startPage(startPage,10);
        return AjaxResult.updateResult(new PageInfo<>(bucketinfoService.searchBucketInfo(userInfoVO.getUserId())));
    }
}



